<?php

class TarefasController extends Zend_Controller_Action
{

    protected $_tarefas;
    
    public function init()
    {
        $this->_tarefas = new Tarefas();
    }

    public function indexAction()
    {
    }

    public function addMetaeventoAction()
    {
        $form = new Application_Form_Tarefa();
        $metaevento_id = $this->getRequest()->getParam('metaevento_id');
        $form->getElement('metaevento_id')->setValue($metaevento_id);
        $form->removeElement('tarefa_status');

        $form->submit->setLabel('Adicionar');
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $identity = Zend_Auth::getInstance()->getIdentity();
                unset($formData['id']);
                unset($formData['projeto_id']);
                unset($formData['evento_id']);
                unset($formData['submit']);
                $formData['gerente_id'] = $identity->id;
                $this->_tarefas->insert($formData);
                $this->_forward('detalhe','metaevento');
            } 
        }
    }

    public function editAction()
    {
        $form = new Application_Form_frmTarefa();
        $form->submit->setLabel('Salvar');
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $this->_tarefas->update(array(
                    'metaevento_id'     => $form->getValue('metaevento_id'),
                    'tarefa'            => $form->getValue('tarefa'),
                    'responsavel_id'    => $form->getValue('responsavel_id'),
                    'dt_estimada'       => $form->getValue('dt_estimada'),
                    'tarefa_status_id'  => $form->getValue('tarefa_status_id'),
                    ),'id='.$form->getValue('id'));
                $this->_forward('detalhe','metaevento');
            }
        } else
        {
            $id = $this->getRequest()->getParam('tarefa_id', 0);
            if ($id > 0) {
                $form->populate($this->_tarefas->find($id)->current()->toArray());
                $locale = new Zend_Locale('pt_BR');
                $dt_estimada = $form->getValue('dt_estimada');
                if($dt_estimada){
                    $date = new Zend_Date($dt_estimada, null , $locale);
                    $dt_estimada = $date->toString('dd/MM/YYYY', null);
                    $form->getElement('dt_estimada')->setValue($dt_estimada);
                }
            }
        }

        $this->view->form = $form;
    }

    public function deleteAction()
    {
        $tarefa_id = $this->getRequest()->getParam('tarefa_id');
        if ($this->getRequest()->isPost())
        {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Sim') {
                $this->_tarefas->delete('id='.$tarefa_id);
            }
                $this->_forward('detalhe','metaevento');
        }
        else {
            $this->view->tarefa = $this->_tarefas->find($tarefa_id)->current();
        }

    }
    
    public function detalheAction()
    {
        $tarefa_id = $this->getRequest()->getParam('tarefa_id');
        if($tarefa_id):
            $table = new Zend_Db_Table('tarefa');
            $tarefa = $table->find($tarefa_id);
        else:
            $this->_redirect('index/index');
        endif;

    }

}

